//
//  UserStandViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/19.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class UserStandViewController: BaseViewController {
    
    let dataList:[NSViewController] = [
        StandAudioViewController(),
        StandFocuViewController(),
        StandListViewController()
    ]
    
    override func loadView() {
        let view = NSView.init(frame: NSRect.init(x: 0, y: 0, width: kStandWidth, height: kStandHight))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.black.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
    }
    
    func setupUI() {
        
        self.view.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addChildrens(vcs: dataList, selectIndex: 1)
        
        self.view.addSubview(userNav)
        userNav.snp.makeConstraints { (make) in
            make.top.centerX.width.equalToSuperview()
            make.height.equalTo(40)
        }
    }
    
    
    lazy var userNav: UserEngineNav = {
        let list:([String],[String]) = (["toolbar_user_menu1","toolbar_user_menu2","toolbar_user_menu3"],["toolbar_user_menu1_select","toolbar_user_menu2_select","toolbar_user_menu3_select"])
        let view = UserEngineNav.init(data: list, index: 1)
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.black.cgColor
        view.indexClick = { index in
            self.switchTo(index: index)
            if index == 1 {
                view.window?.styleMask = [.resizable,.fullSizeContentView]
            }else {
                view.window?.styleMask = [.fullSizeContentView]
            }
        }
        return view
    }()
    
    
    lazy var container: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        return view
    }()
    
    func addChildrens(vcs:[NSViewController],selectIndex:Int?)  {
        for (inx,val) in vcs.enumerated() {
            self.addChild(val)
            if inx == selectIndex {
                self.container.addSubview(val.view)
                val.view.snp.makeConstraints { (make) in
                    make.edges.equalToSuperview().inset(NSEdgeInsetsMake(0, 0, 0, 0))
                }
            }
        }
    }
    
    
    func switchTo(vc:NSViewController) {
        //上一个高度
        let lastHeight = self.container.subviews[0].frame.size.height
        for sView in self.container.subviews {
            sView.removeFromSuperview()
        }
        
        
        self.container.addSubview(vc.view)
        vc.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(0, 0, 0, 0))
        }
        
        // 页面适配
        let x = self.view.window?.frame.origin.x ?? 0
        let y = self.view.window?.frame.origin.y ?? 0
        let ky = y+lastHeight-vc.view.frame.size.height
        self.view.window?.setContentSize(vc.view.frame.size)
        self.view.window?.setFrameOrigin(NSPoint.init(x: x, y:ky))
        
    }
    
    public func switchTo(index:Int) {
        if index > (self.dataList.count-1) {
            return
        }
        let viewController = self.dataList[index]
        self.switchTo(vc: viewController)
    }
    
    
}
